/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.codegen;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import org.xml.sax.InputSource;
import org.zoundry.gaedt.core.ZGaeException;
import org.zoundry.gaedt.core.models.cspec.ZGaeCSpecReader;
import org.zoundry.gaedt.core.models.cspec.ZGaeControllersModel;
import org.zoundry.gaedt.core.util.ZGaeCloser;
import test.org.zoundry.gaedt.core.ZGaeCoreTestCase;


/**
 * Tests the .cspec to .py generator.
 */
public class ZGaeCSpecToPythonGeneratorTest extends ZGaeCoreTestCase
{
    public void testGenerator() throws Exception
    {
        String cspecXML =
            "<cspec:controllers xmlns:cspec=\"urn:com:zoundry:gae:core:controllerSpec:2008:07\" debug=\"true\">\r\n" +
            "   <cspec:controller path=\"/cOne\" class=\"com.zoundry.controllers.Controller1\" />\r\n" +
            "   <cspec:controller path=\"/cTwo\" class=\"com.zoundry.controllers.Controller2\" />\r\n" +
            "   <cspec:controller pattern=\"/*.html\" class=\"com.zoundry.controllers.HtmlController\" />\r\n" +
            "</cspec:controllers>";

        ZGaeControllersModel model = createModel(cspecXML);

        ZGaeCSpecToPythonGenerator generator = new ZGaeCSpecToPythonGenerator();
        String pyXML = generator.generate(model);
        String expected = loadExpected("expected.py");
        assertEquals(expected, pyXML);
    }

    /**
     * Loads the expected results into a String.
     * 
     * @param aResourceName
     * @throws IOException
     */
    private String loadExpected(String aResourceName) throws IOException
    {
        StringBuffer buffer = new StringBuffer();
        synchronized (buffer)
        {
            InputStream iStream = null;
            InputStreamReader iStreamReader = null;
            BufferedReader reader = null;
            try
            {
                iStream = ZGaeCSpecToPythonGeneratorTest.class.getResourceAsStream(aResourceName);
                iStreamReader = new InputStreamReader(iStream);
                reader = new BufferedReader(iStreamReader);
                String str = null;
                while ( (str = reader.readLine()) != null)
                {
                    buffer.append(str);
                    buffer.append("\r\n");
                }
                return buffer.toString();
            }
            finally
            {
                ZGaeCloser.close(iStream);
                ZGaeCloser.close(iStreamReader);
                ZGaeCloser.close(reader);
            }
        }
    }

    /**
     * Creates a model from the given .cspec xml string.
     * 
     * @param aCSpecXML
     * @throws ZGaeException
     */
    private ZGaeControllersModel createModel(String aCSpecXML) throws ZGaeException
    {
        InputSource inputSource = new InputSource(new StringReader(aCSpecXML));
        return (ZGaeControllersModel) ZGaeCSpecReader.getInstance().read(inputSource);
    }
}
